<template>
  <view class="user-info">
    <fui-form ref="form">
      <fui-input required label="收款姓名" borderTop v-model="formData.name" placeholder="请输入收款姓名"></fui-input>
      <fui-input required label="身份证" borderTop v-model="formData.identity" placeholder="请填写正确以免打款失败"></fui-input>
      <fui-input required label="支付账号" borderTop v-model="formData.ali" placeholder="支付宝实名需要与收款人一致"></fui-input>
      <fui-input required label="微信号" borderTop v-model="formData.wx" placeholder="请输入微信号"></fui-input>
      <fui-input required label="手机号" borderTop v-model="formData.mobile" placeholder="账号丢失找回">
        <fui-button type="gray" bold width="200rpx" height="64rpx" :size="28" text="更换手机号"></fui-button>
      </fui-input>
      <fui-input required label="验证码" v-model="formData.code" borderTop placeholder="请输入验证码">
        <fui-button type="gray" @click="getSms" bold width="200rpx" height="64rpx" :size="28" text="获取验证码"></fui-button>
      </fui-input>
      <fui-button :margin="['100rpx','0rpx']"  bold @click="submit" radius="96rpx">立即保存</fui-button>
    </fui-form>
    <fui-toast ref="toast"></fui-toast>
  </view>
</template>

<script setup lang="ts">
import dyUserInfoService from "@/api/dyUserInfoService";
import loginService from '@/api/loginService'
const rules = [
  {
	name: "name",
	rule: ["required"],
	msg: ["请输入姓名"]
}, 
{
	name: "mobile",
	rule: ["required", "isMobile"],
	msg: ["请输入手机号", "请输入正确的手机号"]
},
{
	name: "identity",
	rule: ["required", "isIdCard"],
	msg: ["请输入身份证", "请输入正确的身份证"]
}, {
	name: "ali",
	rule: ["required"],
	msg: ["请输入支付宝号"]
}, 
{
	name: "wx",
	rule: ["required"],
	msg: ["请输入微信号"]
}, {
	name: "code",
	rule: ["required"],
	msg: ["请输入验证码"]
}, 

];
const formData =ref({
  name:"",
  identity:"",
  ali:"",
  wx:"",
  mobile:"",
  code:""
})
const toast = ref()
const form = ref()
type tvalidator = { errorMsg: string; isPassed: boolean }
const submit = ()=>{
  form.value.validator(formData.value, rules).then(async (res:tvalidator) => {
		if (res.isPassed) {
			//this.fui.toast('校验通过！')

      const {success} = await dyUserInfoService.userInfoSaveOrUpdate({
        aliCode:formData.value.ali,
        wxCode: formData.value.wx,
        phone:  formData.value.mobile,
        userName: formData.value.name,
        idCard: formData.value.identity,
        code:formData.value.code
      })
        if(success){


        }
		}
	}).catch((err:any) => {
		console.log(err)
	})
}


const  getUsetInfo = async ()=>{
  uni.showLoading()
  const {success,result} = await  dyUserInfoService.getUserInfo()
  if(success){
    formData.value.ali = result.aliCode
    formData.value.wx = result.wxCode
    formData.value.mobile = result.phone
    formData.value.name = result.userName
    formData.value.identity = result.idCard
  }
}

// 获取验证码
const getSms = async()=>{
  if(!formData.value.mobile){
    uni.showToast({
          title: '手机号为空',
          icon: 'none',
          mask: true,
          duration: 1000
        })
    return
  }
  const {success,result} = await loginService.sms({
      mobile:formData.value.mobile,
      smsmode:3
  })
  if(success){
    uni.showToast({
      title: '手机号发送成功',
      icon: 'none',
      mask: true,
      duration: 1000
    })
  }

  
}

onLoad(()=>{
  getUsetInfo()
  uni.hideLoading()
})

</script>

<style lang="scss" scoped>
.user-info{
  margin: 28rpx;
}
</style>
